<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">合同管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="请输入用户名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="param.password" @keyup.enter.native="submitForm()">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="select-role">
                    <el-radio-group v-model="param.type">
                        <el-radio :label="1">普通用户</el-radio>
                        <el-radio :label="0">系统管理员</el-radio>
                    </el-radio-group>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <div class="reg-btn">
                    <el-button type="primary" @click="handleReg()">注册</el-button>
                </div>
                <!-- <p class="login-tips">Tips : 用户名和密码随便填。</p> -->
            </el-form>
        </div>

        <el-dialog title="注册用户" :visible.sync="regVisible" width="30%">
            <el-form ref="reg" :model="reg" :rules="rules" label-width="70px">
                <el-form-item prop="username" label="用户名">
                    <el-input v-model="reg.username"></el-input>
                </el-form-item>

                <el-form-item prop="password1" label="密码">
                    <el-input v-model="reg.password1" type="password"></el-input>
                </el-form-item>
                <el-form-item prop="password2" label="密码">
                    <el-input v-model="reg.password2" type="password"></el-input>
                </el-form-item>
                <el-form-item prop="phone" label="手机">
                    <el-input v-model="reg.phone"></el-input>
                </el-form-item>
                <!-- <el-form-item prop="depart" label="部门">
                    <el-select v-model="reg.depart" placeholder="请选择">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                    </el-select>
                </el-form-item> -->
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="regVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitReg()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { login, register } from '../../api/index';
export default {
    data: function () {
        return {
            param: {
                username: 'zhangsan',
                password: '123123',
                type: 1
            },
            regVisible: false,
            reg: {
                username: '',
                password1: '',
                password2: '',
                phone: '',
                depart: ''
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                password1: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                password2: [{ required: true, message: '请再次输入密码', trigger: 'blur' }],
                phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
            }

        };
    },
    methods: {
        handleReg() {
            this.regVisible = true;
        },
        submitReg() {
            this.$refs.reg.validate((vaild) => {
                if (vaild) {
                    register({
                        username: this.reg.username,
                        password1: this.reg.password1,
                        password2: this.reg.password2,
                        phone: this.reg.phone
                    })
                        .then((res) => {
                            if (res.code === 200) {
                                this.$message.success(res.msg);
                                this.regVisible = false;
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((err) => {
                            console.log(err);
                            this.$message.error('注册失败');
                        });
                } else {
                    return false;
                }
            });
        },
        submitForm() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    //console.log(this.param.role)
                    login({
                        username: this.param.username,
                        password: this.param.password,
                        type: this.param.type
                    })
                        .then((res) => {
                            if (res.code === 200) {
                                localStorage.setItem('username', this.param.username);
                                localStorage.setItem("usertype",this.param.type);
                                localStorage.setItem("userId",res.data);
                                console.log(res.data);
                                this.$message.success(res.msg)
                                this.$router.push('/');
                            } else {
                                this.$message.error(res.msg);
                            }
                        })
                        .catch((err) => {
                            console.log(err);
                            this.$message.error('接口请求失败');
                        });
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    /* background-image: url(../../assets/img/login-bg.jpg); */

    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.5);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.select-role {
    text-align: center;
    padding: 15px;
}
.login-btn {
    text-align: center;
}
.reg-btn {
    text-align: center;
}

.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.reg-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>